import React, { Component } from 'react'
import './index.css'
import PropTypes from 'prop-types'

export default class Item extends Component {

    static propTypes = {
        updateTodo:PropTypes.func.isRequired
    }

    state = {highLight:false}

    handleMouse(bool){
        return ()=>{
            this.setState({highLight:bool})
        }
    }

    handleChange(id){
        return (e)=>{
            this.props.updateTodo(id,e.target.checked)
        }
    }
    
    handleDelete(id){
        return ()=>{
            this.props.deleteTodo(id)
        }
    }

    render() {
        const {id,name,done} = this.props
        return (
            <li onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)} style={{backgroundColor:this.state.highLight?'gray':'white'}}>
                <input type="checkbox" checked={done} onChange={this.handleChange(id)}/>
                {name}
                <button onClick={this.handleDelete(id)}>删除</button>
            </li>
        )
    }
}
